define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'grhtml', 'CreateControl'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        _queryString: '',
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'tvsd/bx/index' + Controller._queryString,
                    add_url: 'tvsd/bx/add' + Controller._queryString,
                    edit_url: 'tvsd/bx/edit',
                    del_url: 'tvsd/bx/del',
                    sh_url: 'tvsd/bx/sh',
                    fsh_url: 'tvsd/bx/fsh',
                    multi_url: 'tvsd/bx/multi',
                    import_url: 'tvsd/bx/import',
                    table: 'dd_bx',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table',function(event,table){
                //欲要跳转TAB的值
                $('ul.nav-tabs li a[data-value="未核对"]').trigger('click');
                //TAB分类的字段
                $('.form-commonsearch select[name=status]').val("未核对");
            })
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                exportTypes: ['excel'],//导出只显示
                exportOptions: {
                    fileName: '报销管理_' + Moment().format("YYYY-MM-DD"),
                    ignoreColumn: [0,'checkbox','operate'] //默认不导出第一列(checkbox)与操作(operate)列
                },
                columns: [
                    [
                        { checkbox: true },
                        {
                            field: '', title: __('序号'), formatter: function (value, row, index) {
                                var options = table.bootstrapTable('getOptions');
                                var pageNumber = options.pageNumber;

                                var pageSize = options.pageSize == "All" ? 100000 : options.pageSize;

                                return (pageNumber - 1) * pageSize + 1 + index;
                            }, operate: false
                        },
                        // { field: 'id', title: __('Id') },


                        // { field: 'weigh', title: __('Weigh'), operate: false },

                        // { field: 'admin_id', title: __('Admin_id') },

                        // { field: 'parentnode.id', title: __('Parentnode.id') },
                        { field: 'parentnode.ddh', title: __('Parentnode.ddh'), operate: 'LIKE' },
                        { field: 'parentnode.kh', title: __('Parentnode.kh'), operate: 'LIKE' },
                        // { field: 'parentnode.lxr', title: __('Parentnode.lxr'), operate: 'LIKE' },
                        // { field: 'parentnode.dh', title: __('Parentnode.dh'), operate: 'LIKE' },
                        { field: 'parentnode.cd', title: __('Parentnode.cd'), operate: 'LIKE' },
                        { field: 'parentnode.cp', title: __('Parentnode.cp'), operate: 'LIKE' },
                        // { field: 'parentnode.zws', title: __('Parentnode.zws'), operate: 'LIKE' },
                        { field: 'parentnode.jsy1', title: __('Parentnode.jsy1'), operate: 'LIKE' },
                        // { field: 'parentnode.jsydh1', title: __('Parentnode.jsydh1'), operate: 'LIKE' },
                        { field: 'parentnode.jsy2', title: __('Parentnode.jsy2'), operate: 'LIKE' },
                        // { field: 'parentnode.jsydh2', title: __('Parentnode.jsydh2'), operate: 'LIKE' },
                        { field: 'parentnode.kstime', title: __('Parentnode.kstime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false },
                        { field: 'parentnode.jstime', title: __('Parentnode.jstime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false },
                        { field: 'parentnode.scd', title: __('Parentnode.scd'), operate: 'LIKE' },
                        { field: 'parentnode.tj', title: __('Parentnode.tj'), operate: 'LIKE' },
                        { field: 'parentnode.mdd', title: __('Parentnode.mdd'), operate: 'LIKE' },
                        { field: 'parentnode.je', title: __('Parentnode.je') },
                        { field: 'parentnode.bz', title: __('Parentnode.bz'), operate: 'LIKE' },
                        // { field: 'parentnode.status', title: __('Parentnode.status'), formatter: Table.api.formatter.status },
                        { field: 'parentnode.ddr', title: __('Parentnode.ddr'), operate: 'LIKE' },
                        { field: 'cclc', title: __('Cclc') },
                        { field: 'gdlc', title: __('Gdlc') },
                        { field: 'btlc', title: __('Btlc') },
                        { field: 'jy', title: __('Jy'), operate: 'BETWEEN' },
                        { field: 'je', title: __('Je'), operate: 'BETWEEN' },
                        { field: 'tcf', title: __('Tcf'), operate: 'BETWEEN' },
                        { field: 'glf', title: __('Glf'), operate: 'BETWEEN' },
                        { field: 'cz', title: __('Cz'), operate: 'BETWEEN' },
                        { field: 'qt', title: __('Qt'), operate: 'BETWEEN' },
                        { field: 'bz', title: __('Bz'), operate: 'LIKE' },
                        { field: 'status', title: __('Status'), searchList: { "未核对": __('未核对'), "已核对": __('已核对') }, formatter: Table.api.formatter.status },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // { field: 'parentnode.admin_id', title: __('Parentnode.admin_id') },
                        // { field: 'parentnode.weigh', title: __('Parentnode.weigh') },
                        // { field: 'parentnode.createtime', title: __('Parentnode.createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // { field: 'parentnode.updatetime', title: __('Parentnode.updatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // { field: 'parentnode.deletetime', title: __('Parentnode.deletetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons:  {classname: 'btn btn-info btn-xs btn-dialog', icon: 'fa fa-plus', url: 'tvsd/bx/add/pid/{ids}' }, formatter: Table.api.formatter.operate }
                        // { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons: [{ name: 'add', text: __('Add Sub'), classname: 'btn btn-info btn-xs btn-dialog', icon: 'fa fa-plus', url: 'tvsd/bx/add/pid/{ids}' }], formatter: Table.api.formatter.operate }
                        {
                            field: 'buttons',
                            // width: "120px",
                            title: __('操作'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    //编辑页面显示按钮
                                    name: 'edit',
                                    text: __('编辑'),
                                    title: __('编辑'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-pencil',
                                    url: 'tvsd/bx/edit',
                                    visible: function (row) {
                                        if (row.status == '已核对') {
                                            return false;//按钮不显示
                                        } else {
                                            return true;//按钮显示
                                        }

                                    },
                                },
                               
                                {
                                    name: 'dy',
                                    text: __('打印'),
                                    title: __('打印'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-folder-o',
                                    url: 'tvsd/bx/dy?ids={pid}',
                                    visible: function (row) {
                                        if (row.status == '已核对') {
                                            return false;//按钮不显示
                                        } else {
                                            return true;//按钮显示
                                        }

                                    },
                                }
                                ,
                                {
                                    name: 'sh',
                                    text: __('审核'),
                                    title: __('审核'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-folder-o',
                                    url: 'tvsd/bx/sh',
                                    visible: function (row) {
                                        if (row.status == '已核对') {
                                            return false;//按钮不显示
                                        } else {
                                            return true;//按钮显示
                                        }

                                    },

                                },
                                {
                                    name: 'fsh',
                                    text: __('反审核'),
                                    title: __('反审核'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-folder-o',
                                    url: 'tvsd/bx/fsh',
                                    visible: function (row) {
                                        if (row.status == '未核对') {
                                            return false;//按钮不显示
                                        } else {
                                            return true;//按钮显示
                                        }

                                    },

                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'tvsd/bx/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'tvsd/bx/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'tvsd/bx/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        //打印页面
        dy: function () {

            //取网址 ？ 后面的ids

            var ids = getUrlParam('ids');
            // //html5 方式打开报表
            // $(document).ready(function () {
            //     //创建报表显示器，参数指定其在网页中的占位标签的ID，关联的报表模板URL，关联的报表数据URL 
            //     // console.log(ids);
            //     var reportViewer = window.rubylong.grhtml5.insertReportViewer("report_holder", 
            //     "/static/派车调度单.grf", "dysj?ids="+ ids);
            //     //启动报表运行，生成报表 

            //     reportViewer.start();
            // });

            //插件方式打开报表
            $(document).ready(function () {
                InsertPrintViewer({
                    //取网页传参，取？后面部分包括？

                    report: urlAddRandomNo("/static/报销单.grf"),
                    data: 'dysj?ids=' + ids
                });
            });

        },
        add: function () {
            $('#c-pid').val(getUrlParam('ids'));
            $('#c-ddh').val(getUrlParam('dh'));
            // console.log(getUrlParam('dh'));
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        sh: function () {
            Controller.api.bindevent();
        },
        fsh: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                $('#c-gdlc').on("input", function (e) {
                    if ($('#c-cclc').val() > 0 && $('#c-gdlc').val() > 0) {
                        var lc = $('#c-gdlc').val() - $('#c-cclc').val();
                        $('#c-btlc').val(lc)
                    }
                    console.log(this.value);
                });




                Form.api.bindevent($("form[role=form]"));
            },
            queryString: function () {
                return location.search.replace("dialog=1", "").split('&').filter(function (item) {
                    return !!item;
                }).join("&");
            }
        }
    };
    Controller._queryString = Controller.api.queryString();
    return Controller;
});

//获取url中的参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}